package com.iurac.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iurac.domain.User;
import com.iurac.exception.LoginException;
import com.iurac.exception.ManageException;
import com.iurac.service.UserService;
import com.iurac.uitl.CodeUtil;
import com.iurac.uitl.MD5Util;
import com.iurac.uitl.UUIDUtil;
import com.iurac.vo.UserVo;
import org.apache.commons.logging.LogFactoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/login.do")
    @ResponseBody
    public Map<String,Object> login(@RequestParam("account") String account, @RequestParam("password")  String password,
                        @RequestParam("role") String role, @RequestParam("securityCode") String securityCode,
                        HttpSession session) throws JsonProcessingException {
        Map<String,Object> map = new HashMap<>();
        boolean success = false;
        if(!CodeUtil.checkVerifyCode(securityCode,session)){
            map.put("msg", "验证码错误");
        }else {
            try {
                User user = userService.login(account, MD5Util.getMD5(password),role);
                success = true;
                session.setAttribute("userInfo",user);
            } catch (LoginException e) {
                e.printStackTrace();
                map.put("msg", e.getMessage());
            }
        }
        map.put("success", success);

        return map;
    }

    @RequestMapping("/register.do")
    @ResponseBody
    public Map<String,Object> register(User user, HttpSession session) throws JsonProcessingException {
        user.setId(UUIDUtil.getUUID());
        String password = user.getPassword();
        user.setPassword(MD5Util.getMD5(password));
        user.setRole("用户");
        user.setBorrowBookLimit("5");
        user.setBorrowTimeLimit("15");
        boolean success;
        String msg = "";
        try {
            success = userService.register(user);
        } catch (ManageException e) {
            e.printStackTrace();
            success = false;
            msg = e.getMessage();
        }
        Map<String,Object> map = new HashMap<>();
        if(success){
            map.put("success",success);
            session.setAttribute("userInfo",user);
        }else {
            map.put("success",success);
            map.put("msg",msg);
        }

        return map;
    }

    @RequestMapping("/logout.do")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/index.jsp";
    }

    @RequestMapping("/user/getBorrowCountById.do")
    @ResponseBody
    public Map<String,Object> getBorrowCountById(@RequestParam("id") String id) throws JsonProcessingException {
        String count = userService.getBorrowCountById(id);

        Map<String,Object> map = new HashMap<>();
        map.put("count",count);

        return map;
    }

    @RequestMapping("/user/getUsersByCondition.do")
    @ResponseBody
    public Map<String,Object> getUsersByCondition(@RequestParam(value="page") int page,@RequestParam("limit") int limit,
                                      @RequestParam(value="username") String username,@RequestParam("account") String account,
                                      @RequestParam(value="phone") String phone,@RequestParam("sex[]") String[] sex,
                                      @RequestParam(value="role[]") String[] role) throws IOException {
        List<UserVo> userVoList = userService.getUsersByCondition(username,account,phone,sex,role);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",userVoList.size());
        map.put("data",userVoList);

        return map;
    }

    @RequestMapping("/user/getUserByAccount.do")
    @ResponseBody
    public Map<String,Object> getUserByAccount(@RequestParam("account") String account) throws IOException {
        UserVo userVo = userService.getUserByAccount(account);
        Map<String,Object> map = new HashMap<>();
        if(userVo.getId() == null ){
            map.put("success",false);
        }else {
            map.put("success",true);
            map.put("user",userVo);
        }

        return map;
    }


    @RequestMapping("/user/getDateAboutUser.do")
    @ResponseBody
    public Map<String,Object> getDateAboutUser() throws IOException {
        Map<String, Object> map = userService.getDateAboutUser();
        return map;
    }

    @RequestMapping("/user/deleteUserById.do")
    @ResponseBody
    public Map<String,Object> deleteUserById(@RequestParam(value="id") String id) throws IOException {
        boolean success;
        String msg = "";
        try {
            success = userService.deleteUserById(id);
        } catch (ManageException e) {
            e.printStackTrace();
            success = false;
            msg = e.getMessage();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("success", success);
        map.put("msg", msg);

        return map;
    }

    @RequestMapping("/user/updateUser.do")
    @ResponseBody
    public Map<String,Object> updateUser(User user) throws IOException {
        String password = MD5Util.getMD5(user.getPassword());
        user.setPassword(password);
        boolean success;
        try {
            success = userService.updateUser(user);
        } catch (ManageException e) {
            e.printStackTrace();
            success = false;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("success", success);

        return map;
    }

    @RequestMapping("/user/updatePhone.do")
    @ResponseBody
    public Map<String,Object> updatePhone(@RequestParam("id")String id, @RequestParam("phone")String phone,HttpSession session) throws IOException {
        boolean success;
        try {
            success = userService.updatePhone(id,phone);
        } catch (ManageException e) {
            e.printStackTrace();
            success = false;
        }
        if(success){
            User user = userService.getUserById(id);
            session.setAttribute("userInfo",user);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("success", success);

        return map;
    }

    @RequestMapping("/user/updatePassword.do")
    @ResponseBody
    public Map<String,Object> updatePassword(@RequestParam("id")String id, @RequestParam("password")String password,HttpSession session) throws IOException {
        boolean success;
        try {
            success = userService.updatePassword(id,MD5Util.getMD5(password));
        } catch (ManageException e) {
            e.printStackTrace();
            success = false;
        }
        if(success){
            User user = userService.getUserById(id);
            session.setAttribute("userInfo",user);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("success", success);

        return map;
    }

    @RequestMapping("/user/saveUser.do")
    @ResponseBody
    public Map<String,Object> saveUser(User user) throws IOException {
        String password = MD5Util.getMD5(user.getPassword());
        user.setId(UUIDUtil.getUUID());
        user.setPassword(password);
        boolean success;
        String msg = "";
        try {
            success = userService.saveUser(user);
        } catch (ManageException e) {
            e.printStackTrace();
            success = false;
            msg = e.getMessage();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("success", success);
        map.put("msg", msg);

        return map;
    }
}
